home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / mxlibs / dwstk102 / playdwm.c < prev    next >
C/C++ Source or Header  |  1995-04-12  |  6KB  |  244 lines

  1. /******************************************************************************
  2. File:          playdwm.c
  3. Version:     1.02
  4. Tab stops: every 2 columns
  5. Project:     DWM Player
  6. Copyright: 1994-1995 DiamondWare, Ltd.    All rights reserved.
  7. Written:     Keith Weiner & Erik Lorenzen
  8. Purpose:     Contains simple example code to show how to load/play a .DWM file
  9. History:     94/10/21 KW Started
  10.                      95/02/21 EL Finalized
  11.                      95/03/22 EL Finalized for 1.01
  12.                      95/04/11 EL Finalized for 1.02
  13.  
  14. Notes
  15. -----
  16. This code isn't really robust when it comes to standard error checking
  17. and particularly recovery, software engineering technique, etc.  A buffer
  18. is statically allocated.    A better technique would be to use fstat() or stat()
  19. to determine the file's size then malloc(size).  The STK will handle songs
  20. larger than 64K (but not digitized sounds).  Obviously, you'd need to fread()
  21. such a file in chunks, or write some sort of hfread() (huge fread).  Also,
  22. exitting and cleanup is not handled robustly in this code.    The code below can
  23. only be validated by extremely careful scrutiny to make sure each case is
  24. handled properly.  A better method would the use of C's atexit function.
  25.  
  26. But all such code would make this example file less clear; its purpose was
  27. to illustrate how to call the STK, not how to write QA-proof software.
  28. ******************************************************************************/
  29.  
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <conio.h>
  35.  
  36. #include "dws.h"
  37. #include "dwt.h"
  38. #include "err.h"
  39.  
  40.  
  41.  
  42. #define BUFFSIZE 65535        /* If the linker outputs the error */
  43.                                                     /* "stack plus data exceed 64K" */
  44.                                                     /* try reducing BUFFSIZE to about 32K, */
  45.                                                     /* or compile for large model. */
  46. byte _far song[BUFFSIZE];
  47.  
  48.  
  49.  
  50. void main(int argc, char **argv)
  51. {
  52.     FILE                                *fp;
  53.     dws_DETECTOVERRIDES dov;
  54.     dws_DETECTRESULTS     dres;
  55.     dws_IDEAL                     ideal;
  56.     dws_MPLAY                     mplay;
  57.     int                                 input=0;
  58.     word                                musvol=255;     /* Default mxr volume at startup is max */
  59.     word                                songstatus;
  60.  
  61.     printf("\nPLAYDWM 1.02 is Copyright 1994-95 DiamondWare, Ltd.\n");
  62.     printf("All rights reserved.\n\n\n");
  63.  
  64.     if (argc < 2)
  65.     {
  66.         printf("Usage PLAYDWM <dwm-file>\n");
  67.         exit(-1);
  68.     }
  69.  
  70.     fp = fopen(argv[1], "rb");
  71.  
  72.     if (fp == NULL)
  73.     {
  74.         printf("Unable to open %s\n", argv[1]);
  75.         exit(-1);
  76.     }
  77.  
  78.     fread(song, (size_t)BUFFSIZE, 1, fp);  /* if filelen<BUFFSIZE, this works */
  79.  
  80.     fclose(fp);
  81.  
  82.     /*
  83.      . We need to set every field to -1 in dws_DETECTOVERRIDES struct;
  84.      . this tells the STK to autodetect everything.  Any other value
  85.      . overrides the autodetect routine, and will be accepted on
  86.      . faith, though the STK will verify it if possible.
  87.     */
  88.     dov.baseport = (word)-1;
  89.     dov.digdma     = (word)-1;
  90.     dov.digirq     = (word)-1;
  91.  
  92.     if (!dws_DetectHardWare(&dov, &dres))
  93.     {
  94.         err_Display(dws_ErrNo());
  95.         exit(-1);
  96.     }
  97.  
  98.     if (!(dres.capability & dws_capability_FM))
  99.     {
  100.         printf("FM support not found\n");
  101.         exit(-1);
  102.     }
  103.  
  104.     /*
  105.      . The "ideal" struct tells the STK how you'd like it to initialize the
  106.      . sound hardware.    In all cases, if the hardware won't support your
  107.      . request, the STK will go as close as possible.  For example, not all
  108.      . sound boards will support all sampling rates (some only support five or
  109.      . six discrete rates).
  110.     */
  111.     ideal.musictyp     = 1;         /* for now, it's OPL2 music */
  112.     ideal.digtyp         = 0;         /* 0=No Dig, 8=8bit, 16=16bit */
  113.     ideal.digrate      = 0;         /* sampling rate, in Hz */
  114.     ideal.dignvoices = 0;         /* number of voices (up to 16) */
  115.     ideal.dignchan     = 0;         /* 1=mono, 2=stereo */
  116.  
  117.     if (!dws_Init(&dres, &ideal))
  118.     {
  119.         err_Display(dws_ErrNo());
  120.         exit(-1);
  121.     }
  122.  
  123.     /*
  124.      . 72.8Hz is a decent compromise.  It will work in a Windows DOS box
  125.      . without any problems, and yet it allows music to sound pretty good.
  126.      . In my opinion, there's no reason to go lower than 72.8 (unless you
  127.      . don't want the hardware timer reprogrammed)--music sounds kinda chunky
  128.      . at lower rates.    You can go to 145.6 Hz, and get smoother (very
  129.      . subtly) sounding music, at the cost that it will NOT run at the correct
  130.      . (or constant) speed in a Windows DOS box.
  131.     */
  132.     dwt_Init(dwt_72_8HZ);
  133.  
  134.     /* Set Music Volume to about 80% max */
  135.     musvol = 200;
  136.  
  137.     if (!dws_XMusic(musvol))
  138.     {
  139.         err_Display(dws_ErrNo());
  140.     }
  141.  
  142.     mplay.track = song;
  143.     mplay.count = 1;                    /* 0=infinite loop, 1-N num times to play sound */
  144.  
  145.     if (!dws_MPlay(&mplay))
  146.     {
  147.         err_Display(dws_ErrNo());
  148.         goto KillIt;
  149.     }
  150.  
  151.     /*
  152.      . We're playing.  Until the song is over, let's allow the user
  153.      . to fiddle with the volume level (mixer) in the meantime
  154.     */
  155.  
  156.     if (!dws_MSongStatus(&songstatus))
  157.     {
  158.         err_Display(dws_ErrNo());
  159.         goto KillIt;
  160.     }
  161.  
  162.     printf("Press + or - to change playback volume \n");
  163.  
  164.     while (songstatus)
  165.     {
  166.         if (kbhit())
  167.         {
  168.             input = getch();
  169.         }
  170.         else
  171.         {
  172.             input = 0;
  173.         }
  174.  
  175.         switch (input)
  176.         {
  177.             case 'q':
  178.             case 'Q':
  179.             case 27:                                /* ESC */
  180.             {
  181.                 if (!dws_MClear())
  182.                 {
  183.                     err_Display(dws_ErrNo());
  184.                 }
  185.  
  186.                 break;
  187.             }
  188.             case '+':
  189.             {
  190.                 musvol++;
  191.  
  192.                 printf("Music Volume is %d\n", musvol);
  193.  
  194.                 if (!dws_XMusic(musvol))
  195.                 {
  196.                     err_Display(dws_ErrNo());
  197.                 }
  198.  
  199.                 break;
  200.             }
  201.             case '-':
  202.             {
  203.                 musvol--;
  204.  
  205.                 printf("Music Volume is %d\n", musvol);
  206.  
  207.                 if (!dws_XMusic(musvol))
  208.                 {
  209.                     err_Display(dws_ErrNo());
  210.                 }
  211.  
  212.                 break;
  213.             }
  214.         }
  215.  
  216.         if (!dws_MSongStatus(&songstatus))
  217.         {
  218.             err_Display(dws_ErrNo());
  219.             goto KillIt;
  220.         }
  221.     }
  222.  
  223.     KillIt:
  224.  
  225.     /* If dwt is not inited calling dwt_Kill will have no effect */
  226.     dwt_Kill();
  227.  
  228.     if (!dws_Kill())
  229.     {
  230.         /*
  231.          . If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  232.          . or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  233.          . must remove his tsr, and dws_Kill must be called again.    If it's
  234.          . dws_NOTINITTED, there's nothing to worry about at this point.
  235.         */
  236.         err_Display(dws_ErrNo());
  237.  
  238.         if (dws_ErrNo() ==    dws_Kill_CANTUNHOOKISR)
  239.         {
  240.             goto KillIt;
  241.         }
  242.     }
  243. }
  244.